import CanvasAbstract from './canvasAbstract'
import bullet from '../model/bullet'
import tank from './tank'
import play from './play'
import audio from '../service/audio'

class Bullet extends CanvasAbstract implements ICanvas {
  intervalId = 0
  num(): number {
    return 10
  }
  model(): BulletModelConstructor {
    return bullet
  }
  render(): void {
    this.intervalId = setInterval(() => {
      this.createBullet()
      this.renderModels()
    }, 50)
  }
  createBullet() {
    tank.models.forEach((tank) => {
      const isExits = this.models.some((m) => m.tank === tank)
      if (!isExits && tank) {
        this.models.push(new bullet(tank))
      }
    })
  }
  addPlayerBullet() {
    play.models[0] && this.models.push(new bullet(play.models[0]))
    audio.fire()
  }
  stop(): void {
    clearInterval(this.intervalId)
  }
}

export default new Bullet('bullet')
